import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {UserService} from "../../../providers/user-service";
import {TabsPage} from "../../../pages/tabs/tabs";
import {FindPwd} from "../find-pwd/find-pwd";
import {Device} from "@ionic-native/device";


export interface UserInfo {
    userName: string;
    pwd: string;
}

@Component({
    selector: 'user-login',
    templateUrl: 'user-login.html',
})
export class UserLogin {
    loginError = false;
    errMsg = '';

    userInfo: UserInfo = {
        userName: '',
        pwd: ''
    };

    constructor(public navCtrl: NavController,
                private device: Device,
                private userServ: UserService) {
    }

    findPwd() {
        this.navCtrl.push(FindPwd);
    }

    login() {
        if (this.userInfo.userName == '' || this.userInfo.pwd == '') {
            this.loginError = true;
            this.errMsg = "用户名、密码不能为空！";
        } else {
            let param = {
                "username": this.userInfo.userName,
                "password": this.userInfo.pwd
            };
            this.userServ.login(param)
                .then(data => {
                    console.info("login，登录成功：", data)
                    this.navCtrl.setRoot(TabsPage);
                })
                .catch(err => {
                    console.error("login，登录失败：",err);
                    this.loginError = true;
                    this.errMsg = err.errorMassage;
                });
        }
    }

}
